package com.itheima.controller;

import com.itheima.autoconfig.oss.OssTemplate;
import com.itheima.common.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class OssController {

    @Autowired
    OssTemplate ossTemplate;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    //图片上传
    @PostMapping("/aliyun/oss/upload")
    public R<Map<String,String>> upload(MultipartFile photoFile) throws IOException {
        String url = ossTemplate.upload(photoFile.getOriginalFilename(), photoFile.getInputStream());
        Map<String,String> map=new HashMap<>();
        map.put("url",url);

        stringRedisTemplate.opsForValue().set("url",url);

        return R.success(map);
    }
}
